<?php
/**
 * 切记：使用PHP7版本执行，PHP8不兼容
 * /Applications/phpstudy/Extensions/php/php7.3.11/bin/php download_page_parser.php
 */

require_once('../vendor/autoload.php');

use Beanbun\Beanbun;
use Beanbun\Middleware\Parser;


/**
 * 调试日志
 * @description: 日志输出
 * @param string $msg
 */
function log4file($msg)
{
    $dateTime = date('Y-m-d H:i:s');
    $info = "[{$dateTime}] {$msg}\r\n";
    error_log($info, 3, "errors.log");
}


//// Beanbun对象
$beanbun = new Beanbun;
$beanbun->name = 'page_parser';
$beanbun->seed = 'https://www.lanmper.cn/mysql/c298';

//// Beanbun解析器
$beanbun->fields = [
    [
        'name' => 'title',
        'selector' => ['.topicLayer h1', 'text']
    ]
];
$parser = new Parser();
$beanbun->middleware($parser);

$beanbun->afterDownloadPage = function($b) {
    //// 下载网页内容
    // file_put_contents(__DIR__.'/download4page/'.md5($b->url).'.page.html', $b->page);

    print_r($b->data);
};
$beanbun->start();


